package com.entfrm.system.controller;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entfrm.base.config.GlobalConfig;
import com.entfrm.system.dto.TokenParam;

import com.entfrm.system.dto.TokenResult;
import com.entfrm.system.dto.UserResult;

import com.entfrm.system.entity.AppApplication;
import com.entfrm.system.entity.Application;
import com.entfrm.system.service.AppApplicationService;
import com.entfrm.system.service.ApplicationService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/appsso")
public class AppSingleLogIn {
    private final AppApplicationService appApplicationService;
    private QueryWrapper<AppApplication> getQueryWrapper(AppApplication appApplication) {
        return new QueryWrapper<AppApplication>()
                .eq(StrUtil.isNotBlank(appApplication.getId()+""), "id", appApplication.getId())
                .orderByAsc("create_time");
    }

    @GetMapping("/testApp")
     public String testApp(String code) {
         System.out.println("---开始进行请求 ----");
        System.out.println("code信息:"+code);
         System.out.println("---根据code获取token----");
         //获取token
         TokenParam tokenParam=new TokenParam();
         tokenParam.setClient_id("mxhy");
         tokenParam.setClient_secret("179FACD4A0A303F61A565069D49E49");
       System.out.println("clinet_id=mxtest sercrt=C1C9BF72FDA690ED780B97A520D4A8");
         tokenParam.setCode(code);
         tokenParam.setRedirect_uri("http://10.20.10.92:8888/appsso/testApp");
         tokenParam.setRefreshToken("");
         tokenParam.setScope("OpenApiScope");
         tokenParam.setResponse_type("authorization_code");
        UserResult userResult=AppSingleLogIn.getUserInfo(tokenParam);
        System.out.println("用户信息:"+JSONUtil.toJsonStr(userResult));
        if(userResult.getData()!=null){
            System.out.println("用户获取的token:"+JSONUtil.toJsonStr(userResult.getData().getItems()));
            if(userResult.getData().getItems().size()>0){
                System.out.println("跳转成功:"+"redirect:https://10.20.3.63:9058/test/#/?loginName="+userResult.getData().getItems().get(0).getItem2().getLoginname());
                return "https://10.20.10.92:9058/h5/#/?loginName="+userResult.getData().getItems().get(0).getItem2().getLoginname();
            }else{
                System.out.println("跳转失败1:"+"redirect:https://10.20.3.63:9058/test/#/error/error");
                return "https://10.20.10.92:9058/h5/#//error/error";
            }
        }else{
            System.out.println("跳转失败2:"+"redirect:https://10.20.3.63:9058/test/#/error/error");
            return "https://10.20.10.92:9058/h5/#/pages/error/error";
        }

     }

  public static  UserResult getUserInfo(TokenParam tokenParam){
         UserResult userResult=new UserResult();
      String url = GlobalConfig.tokenUrl();
      String json = JSONUtil.toJsonStr(tokenParam);
      ObjectMapper mapper = new ObjectMapper();
      HttpResponse response2 = HttpRequest.post(url)
              .header("Content-Type", "application/json")
              .body(json)
              .execute();
      if (response2.getStatus() == 200) {
          JSONObject jsonObject = JSONUtil.parseObj(response2.body());
          TokenResult tokenResult = JSONUtil.toBean(jsonObject, TokenResult.class);
          //已获取到token
          if (tokenResult.getData().getItems().size() == 0) {
              System.out.println("code失效");
          } else {
              String accessToken = tokenResult.getData().getItems().get(0).getItem2().getAccessToken();
              //获取到token后获取用户名
              String getUserUrl = GlobalConfig.urlName();
              HttpResponse response1 = HttpRequest.get(getUserUrl)
                      .header("Authorization", "Bearer " + accessToken)
                      .execute();
                userResult = JSONUtil.toBean(JSONUtil.parseObj(response1.body()), UserResult.class);

          }

      }
      return userResult;
  }
}
